<template>
  <div class="app-container">
    <el-form :model="queryParams" ref="queryForm" size="small" :inline="true" v-show="showSearch" label-width="68px">
      <el-form-item label="抽样方案编码" prop="FNumber" label-width="110px">
        <el-input v-model="queryParams.FNumber" placeholder="请输入抽样方案编码" clearable @keyup.enter.native="handleQuery" />
      </el-form-item>
      <!--        <el-form-item label="严格度" prop="FStrictLevel">-->
      <!--          <el-input-->
      <!--            v-model="queryParams.FStrictLevel"-->
      <!--            placeholder="请输入严格度"-->
      <!--            clearable-->
      <!--            @keyup.enter.native="handleQuery"-->
      <!--          />-->
      <!--        </el-form-item>-->
      <el-form-item label="抽样方案名称" prop="FName" label-width="110px">
        <el-input v-model="queryParams.FName" placeholder="请输入抽样方案名称" clearable @keyup.enter.native="handleQuery" />
      </el-form-item>
      <el-form-item>
        <el-button type="primary" icon="el-icon-search" size="mini" @click="handleQuery">搜索</el-button>
        <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
      </el-form-item>
    </el-form>

    <el-row :gutter="10" class="mb8">
      <!-- <el-col :span="1.5">
        <el-button
          type="primary"
          plain
          icon="el-icon-plus"
          size="mini"
          @click="handleAdd"
          v-hasPermi="['mes:qc:sample:add]"
        >新增
        </el-button>
      </el-col> -->
      <!-- <el-col :span="1.5">
        <el-button
          type="success"
          plain
          icon="el-icon-edit"
          size="mini"
          :disabled="single"
          @click="handleUpdate"
          v-hasPermi="['mes:qc:sample:edit']"
        >修改
        </el-button>
      </el-col> -->
      <el-col :span="1.5">
        <el-button type="danger" plain icon="el-icon-delete" size="mini" :disabled="multiple" @click="handleDelete" v-hasPermi="['mes:qc:sample:delete']">删除
        </el-button>
      </el-col>
      <el-col :span="1.5">
        <!-- <el-button
          type="warning"
          plain
          icon="el-icon-download"
          size="mini"
          @click="handleExport"
          v-hasPermi="['system:samplescheme:export']"
        >导出
        </el-button> -->
        <el-button type="primary" plain icon="el-icon-plus" size="mini" @click="handleSet" v-hasPermi="['mes:qc:sample:sync']">同步数据</el-button>
      </el-col>
      <right-toolbar :showSearch.sync="showSearch" @queryTable="getList"></right-toolbar>
    </el-row>

    <el-table v-loading="loading" :data="sampleschemeList" @selection-change="handleSelectionChange">
      <el-table-column type="selection" width="55" align="center" />
      <el-table-column label="序号" align="center" width="50px">
        <template slot-scope="scope">
          <span>{{ scope.$index + 1 }}</span>
        </template>
      </el-table-column>
      <el-table-column label="编码" align="center" prop="fnumber" />
      <el-table-column label="名称" align="center" prop="fname" />
      <el-table-column label="抽样次数" align="center" prop="fsampleTimes" width="75px" />
      <el-table-column label="检验水平" align="center" prop="finspectLevel" />
      <el-table-column label="严格度" align="center" prop="fstrictLevel" />
      <el-table-column label="AQL" align="center" prop="faql" />
      <el-table-column label="系统预设" align="center" prop="fisSystemSet">
        <template slot-scope="scope">
          <el-switch v-model="scope.row.fisSystemSet" active-value="true" inactive-value="false" disabled></el-switch>
        </template>
      </el-table-column>
      <!--      <el-table-column label="系统预设" align="center" prop="fisSystemSet" />-->

      <el-table-column label="批量" align="center" prop="fbatch" />
      <el-table-column label="样本量" align="center" prop="fsampleQty" />
      <el-table-column label="允收数" align="center" prop="facceptQty" />
      <el-table-column label="拒收数" align="center" prop="frejectQty" />
      <el-table-column label="样本量字码" align="center" prop="fsampleCode" />
      <el-table-column label="样本量比率(%)" align="center" prop="fsampleRate" />
      <el-table-column label="样本下限" align="center" prop="fsampleLow" />
      <el-table-column label="样本上限" align="center" prop="fsampleUp" />
      <!--      <el-table-column label="公式" align="center" prop="fexpression" />-->
      <!--      <el-table-column label="描述" align="center" prop="fdescription" />-->
      <el-table-column label="操作" align="center" class-name="small-padding fixed-width">
        <template slot-scope="scope">
          <el-button size="mini" type="text" icon="el-icon-edit" @click="handleUpdate(scope.row)" v-hasPermi="['mes:qc:sample:edit']">修改
          </el-button>
          <el-button size="mini" type="text" icon="el-icon-delete" @click="handleDelete(scope.row)" v-hasPermi="['mes:qc:sample:delete']">删除
          </el-button>
        </template>
      </el-table-column>
    </el-table>

    <pagination v-show="total > 0" :total="total" :page.sync="queryParams.pageNum" :limit.sync="queryParams.pageSize" @pagination="getList" />

    <!-- 添加或修改抽样方案对话框 -->
    <el-dialog :title="title" :visible.sync="open" width="650px" append-to-body>
      <el-form ref="form" :model="form" :rules="rules" label-width="120px">
        <el-row>
          <el-col :span="12">
            <el-form-item label="抽样方案编码" prop="fnumber">
              <el-input v-model="form.fnumber" placeholder="请输入抽样方案编码" />
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="名称" prop="fname">
              <el-input v-model="form.fname" placeholder="请输入名称" />
            </el-form-item>
          </el-col>
        </el-row>

        <el-row>
          <el-col :span="12">
            <el-form-item label="检验水平" prop="finspectLevel">
              <el-input v-model="form.finspectLevel" placeholder="请输入检验水平" />
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="严格度" prop="fstrictLevel">
              <el-input v-model="form.fstrictLevel" placeholder="请输入严格度" />
            </el-form-item>
          </el-col>
        </el-row>
        <el-row>
          <el-col :span="12">
            <el-form-item label="AQL" prop="faql">
              <el-input v-model="form.faql" placeholder="请输入AQL" />
            </el-form-item>
          </el-col>

          <el-col :span="12">
            <el-form-item label="系统预设" prop="fisSystemSet">
              <!--              <template slot-scope="scope">-->
              <!--                <el-switch-->
              <!--                  v-model="scope.row.fisSystemSet"-->
              <!--                  active-value="true"-->
              <!--                  inactive-value="false"-->
              <!--                >-->
              <!--                </el-switch>-->
              <!--              </template>-->
              <el-switch v-model="form.fisSystemSet" active-value="true" inactive-value="false">
              </el-switch>
              <!--              <el-input v-model="form.fisSystemSet" placeholder="请输入系统预设"/>-->
            </el-form-item>
          </el-col>
        </el-row>
        <el-row>
          <!--          <el-col :span="12">-->
          <!--            <el-form-item label="系统预设" prop="fisSystemSet">-->
          <!--              <el-input v-model="form.fisSystemSet" placeholder="请输入系统预设"/>-->
          <!--            </el-form-item>-->
          <!--          </el-col>-->
          <el-col :span="12">
            <el-form-item label="公式" prop="fexpression">
              <el-input v-model="form.fexpression" placeholder="请输入公式" />
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="抽样次数" prop="fsampleTimes">
              <el-input v-model="form.fsampleTimes" placeholder="请输入抽样次数" />
            </el-form-item>
          </el-col>

        </el-row>

        <el-row>
          <el-col :span="12">
            <el-form-item label="批量" prop="fbatch">
              <el-input v-model="form.fbatch" placeholder="请输入批量" />
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="样本量" prop="fsampleQty">
              <el-input v-model="form.fsampleQty" placeholder="请输入样本量" />
            </el-form-item>
          </el-col>
        </el-row>

        <el-row>
          <el-col :span="12">
            <el-form-item label="允收数" prop="facceptQty">
              <el-input v-model="form.facceptQty" placeholder="请输入允收数" />
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="拒收数" prop="frejectQty">
              <el-input v-model="form.frejectQty" placeholder="请输入拒收数" />
            </el-form-item>
          </el-col>
        </el-row>

        <el-row>
          <el-col :span="12">
            <el-form-item label="样本量字码" prop="fsampleCode">
              <el-input v-model="form.fsampleCode" placeholder="请输入样本量字码" />
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="样本量比率(%)" prop="fsampleRate">
              <el-input v-model="form.fsampleRate" placeholder="请输入样本量比率(%)" />
            </el-form-item>
          </el-col>
        </el-row>

        <el-row>
          <el-col :span="12">
            <el-form-item label="样本下限" prop="fsampleLow">
              <el-input v-model="form.fsampleLow" placeholder="请输入样本下限" />
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="样本上限" prop="fsampleUp">
              <el-input v-model="form.fsampleUp" placeholder="请输入样本上限" />
            </el-form-item>
          </el-col>
        </el-row>

        <el-row>
          <!--          <el-col :span="12">-->
          <!--            <el-form-item label="公式" prop="fexpression">-->
          <!--              <el-input v-model="form.fexpression" placeholder="请输入公式"/>-->
          <!--            </el-form-item>-->
          <!--          </el-col>-->
          <!--          <el-col :span="12">-->
          <el-form-item label="描述" prop="fdescription">
            <el-input type="textarea" v-model="form.fdescription" placeholder="请输入描述" />
          </el-form-item>
          <!--          </el-col>-->
        </el-row>

      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="submitForm">确 定</el-button>
        <el-button @click="cancel">取 消</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import {
  listSamplescheme,
  getSamplescheme,
  delSamplescheme,
  addSamplescheme,
  updateSamplescheme
} from "@/api/mes/qc/samplescheme";
import { getJdySamplescheme } from "../../../../api/mes/qc/samplescheme";

export default {
  name: "Samplescheme",
  data() {
    return {
      // 遮罩层
      loading: true,
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 抽样方案表格数据
      sampleschemeList: [],
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        fid: null,
        FEntity_FEntryID: null,
        FNumber: null,
        FSampleTimes: null,
        FInspectLevel: null,
        FStrictLevel: null,
        FSampleType: null,
        FAQL: null,
        FIsSystemSet: null,
        FName: null,
        FDescription: null,
        FBatch: null,
        FSampleQty: null,
        FAcceptQty: null,
        FRejectQty: null,
        FSampleCode: null,
        FSampleRate: null,
        FSampleLow: null,
        FSampleUp: null,
        FExpression: null
      },
      // 表单参数
      form: {},
      // 表单校验
      rules: {
        //   fid: [
        //   { required: true, message: "金蝶云抽样方案fid不能为空", trigger: "blur" }
        // ],
        // fentityFentryid: [
        //   { required: true, message: "金蝶云明细id不能为空", trigger: "blur" }
        // ],
      }
    };
  },
  created() {
    this.getList();
  },
  methods: {
    /** 查询抽样方案列表 */
    getList() {
      this.loading = true;
      listSamplescheme(this.queryParams).then(response => {
        this.sampleschemeList = response.rows;
        this.total = response.total;
        this.loading = false;
      });
    },
    // 取消按钮
    cancel() {
      this.open = false;
      this.reset();
    },
    // 表单重置
    reset() {
      this.form = {
        id: null,
        fid: null,
        fentityFentryid: null,
        fnumber: null,
        fsampleTimes: null,
        finspectLevel: null,
        fstrictLevel: null,
        fsampleType: null,
        faql: null,
        fisSystemSet: null,
        fname: null,
        fdescription: null,
        fbatch: null,
        fsampleQty: null,
        facceptQty: null,
        frejectQty: null,
        fsampleCode: null,
        fsampleRate: null,
        fsampleLow: null,
        fsampleUp: null,
        fexpression: null,
        createBy: null,
        createTime: null,
        updateBy: null,
        updateTime: null
      };
      this.resetForm("form");
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1;
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.resetForm("queryForm");
      this.handleQuery();
    },
    // 多选框选中数据
    handleSelectionChange(selection) {
      this.ids = selection.map(item => item.id);
      this.single = selection.length !== 1;
      this.multiple = !selection.length;
    },
    /** 新增按钮操作 */
    handleAdd() {
      this.reset();
      this.open = true;
      this.title = "添加抽样方案";
    },
    /** 修改按钮操作 */
    handleUpdate(row) {
      this.reset();
      const id = row.id || this.ids;
      getSamplescheme(id).then(response => {
        this.form = response.data;
        this.open = true;
        this.title = "修改抽样方案";
      });
    },
    /** 提交按钮 */
    submitForm() {
      this.$refs["form"].validate(valid => {
        if (valid) {
          if (this.form.id != null) {
            updateSamplescheme(this.form).then(response => {
              this.$modal.msgSuccess("修改成功");
              this.open = false;
              this.getList();
            });
          } else {
            addSamplescheme(this.form).then(response => {
              this.$modal.msgSuccess("新增成功");
              this.open = false;
              this.getList();
            });
          }
        }
      });
    },
    /** 删除按钮操作 */
    handleDelete(row) {
      const ids = row.id || this.ids;
      const fnumber = row.fnumber;
      this.$modal
        .confirm('是否确认删除抽样方案编码为"' + fnumber + '"的数据项？')
        .then(function () {
          return delSamplescheme(ids);
        })
        .then(() => {
          this.getList();
          this.$modal.msgSuccess("删除成功");
        })
        .catch(() => {
        });
    },
    /** 导出按钮操作 */
    handleExport() {
      this.download(
        "mes/qc/samplescheme/export",
        {
          ...this.queryParams
        },
        `samplescheme_${new Date().getTime()}.xlsx`
      );
    },
    async handleSet() {
      try {
        const response = await getJdySamplescheme();
        console.info("response.data", response.data);
        if (response.code === 200) {
          this.$message({
            message: "数据同步成功",
            type: "success"
          });
        } else {
          throw new Error("数据同步失败");
        }
      } catch (err) {
        this.$message.error(err.message);
      } finally {
        this.loading = false;
      }
    }
  }
  /** 同步数据按钮操作 */
};
</script>
